package com.ebt.m.proposal_v2.utils;

import com.ebt.m.proposal_v2.bean.ApplicantEntity;
import com.ebt.m.proposal_v2.bean.InsuredEntity;
import com.ebt.m.proposal_v2.ui.view.InsuranceView;
import com.ebt.m.proposal_v2.ui.view.InsuredView;
import com.ebt.m.proposal_v2.widget.view.InsuranceCellView;

import java.util.ArrayList;

/**
 * Created by Rick.Wang on 2017/11/23.
 */

public class InsuredCheckUtils {

    public static ArrayList<Object> checkChangeAccordWithInsuranceLimit(boolean isInsuredPerson, InsuredView insuredView, InsuredEntity insuredEntity, int changedType) {
        boolean retCheckBoolean = true;
        StringBuilder retCheckString = new StringBuilder();
        ArrayList<Object> ret = new ArrayList<>();
        if (insuredView != null) {
            InsuranceView parentInsuranceView = insuredView.getCorrespondingInsuranceView();
            if (parentInsuranceView != null) {
                ArrayList<InsuranceCellView> cellViews = parentInsuranceView.getInsuranceCellViews();
                for (InsuranceCellView cellView : cellViews) {
                    InsuredEntity insuredEntityTem = getApplicantAsInsuredEntity(isInsuredPerson, insuredEntity, parentInsuranceView, cellView);
                    ArrayList<Object> retCellCheck = cellView.checkIfMatchCondition(insuredEntityTem);
                    if (!(Boolean) retCellCheck.get(0)) {
                        if (changedType == (Integer) retCellCheck.get(2)) {
                            retCheckBoolean = false;
                            retCheckString.append(retCellCheck.get(1));
                            break;
                        }
                    }
                }
            }
        }
        ret.add(retCheckBoolean);
        ret.add(retCheckString.toString());
        return ret;
    }

    public static InsuredEntity getApplicantAsInsuredEntity(boolean isInsuredPerson, InsuredEntity insuredEntity, InsuranceView parentInsuranceView, InsuranceCellView cellView) {
        InsuredEntity applicantEntityTem = null;
        if (cellView.isExemption() && cellView.getExemptionType() == 1 && isInsuredPerson) {//豁免投保人则获取投保人的年龄、性别、职业类别校验
            ApplicantEntity applicantEntity = parentInsuranceView.getApplicantPerson();
            applicantEntityTem = new InsuredEntity();
            applicantEntityTem.age = applicantEntity.age;
            applicantEntityTem.birthday = applicantEntity.birthday;
        } else {
            applicantEntityTem = insuredEntity;
        }
        return applicantEntityTem;
    }
}
